//
//  PhotoTableViewCell.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/14.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "PhotoTableViewCell.h"

@interface PhotoTableViewCell()
{
    UIImageView * image1;
    UIImageView * image2;
    UIImageView * image3;
    UIImageView * image4;
    UIView * line;
    NSArray * contentData;
}
@end

@implementation PhotoTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth - 20, 20)];
        titleLabel.font = [UIFont boldSystemFontOfSize:15];
        titleLabel.text = @"过往照片";
        titleLabel.textColor = [UIColor colorFromHexCode:@"a3a3a3"];
        [self addSubview:titleLabel];
        
        image1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 40, (ScreenWidth - 20)/4 - 10, (ScreenWidth - 20)/4 - 10)];
        image1.tag = 1;
        [self addSubview:image1];
        
        image2 = [[UIImageView alloc]initWithFrame:CGRectMake(10 + (ScreenWidth - 20)/4, 40, (ScreenWidth - 20)/4 - 10, (ScreenWidth - 20)/4 - 10)];
        image2.tag = 2;
        [self addSubview:image2];

        image3 = [[UIImageView alloc]initWithFrame:CGRectMake(10 + (ScreenWidth - 20)/4 + (ScreenWidth - 20)/4, 40, (ScreenWidth - 20)/4 - 10, (ScreenWidth - 20)/4 - 10)];
        image3.tag = 3;
        [self addSubview:image3];

        image4 = [[UIImageView alloc]initWithFrame:CGRectMake(10 + (ScreenWidth - 20)/4 + (ScreenWidth - 20)/4 + (ScreenWidth - 20)/4, 40, (ScreenWidth - 20)/4 - 10, (ScreenWidth - 20)/4 - 10)];
        image4.tag = 4;
        [self addSubview:image4];
        
        image1.contentMode = UIViewContentModeScaleAspectFill;
        image1.clipsToBounds = YES;
        image2.contentMode = UIViewContentModeScaleAspectFill;
        image2.clipsToBounds = YES;
        image3.contentMode = UIViewContentModeScaleAspectFill;
        image3.clipsToBounds = YES;
        image4.contentMode = UIViewContentModeScaleAspectFill;
        image4.clipsToBounds = YES;
        
        image1.userInteractionEnabled = YES;
        image2.userInteractionEnabled = YES;
        image3.userInteractionEnabled = YES;
        image4.userInteractionEnabled = YES;

        UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectImage:)];
        [image1 addGestureRecognizer:tap1];
        UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectImage:)];
        [image2 addGestureRecognizer:tap2];
        UITapGestureRecognizer * tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectImage:)];
        [image3 addGestureRecognizer:tap3];
        UITapGestureRecognizer * tap4 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectImage:)];
        [image4 addGestureRecognizer:tap4];
        
        line = [[UIView alloc]initWithFrame:CGRectMake(0, (ScreenWidth - 20)/4 + 40, ScreenWidth, 5)];
        line.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
        [self addSubview:line];
    }
    return self;
}

+(CGFloat)heightWithData:(NSArray *)data
{
    if ([data count])
    {
        return (ScreenWidth - 20)/4 + 45;
    }
    else
    {
        return 45;
    }
}

-(void)loadData:(NSArray *)data
{
    contentData = data;
    if ([data count])
    {
        switch ([data count]) {
            case 4:
                [image4 sd_setImageWithURL:[NSURL URLWithString:[data objectAtIndex:3]]];

            case 3:
                [image3 sd_setImageWithURL:[NSURL URLWithString:[data objectAtIndex:2]]];

            case 2:
                [image2 sd_setImageWithURL:[NSURL URLWithString:[data objectAtIndex:1]]];

            case 1:
                [image1 sd_setImageWithURL:[NSURL URLWithString:[data objectAtIndex:0]]];
            default:
                break;
        }
        line.frame = CGRectMake(0, (ScreenWidth - 20)/4 + 40, ScreenWidth, 5);
    }
    else
    {
        line.frame = CGRectMake(0, 40, ScreenWidth, 5);
    }
}

-(void)selectImage:(UITapGestureRecognizer *)button
{
    [self.delegate selectedImage:(button.view.tag - 1) images:contentData];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
